// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:

import RedpointStyle from "./RedpointStyle";
import RedpointSystem from "./RedpointSystem";

//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
export default class RedpointNode {
    public redpointNums: number = 0;
    public name: string = null;
    public cfg: any = null;
    public parent: RedpointNode = null;
    public children: RedpointNode[] = [];
    public target: cc.Node = null;
    public redpointStyle: RedpointStyle = null;

    constructor(name: string, config?: any) {
        this.name = name;
        this.cfg = config;

        if (this.cfg && this.cfg.isLeafNode == 1) {
            //注册根节点事件
            this.registerEvent();
        }
    }

    /**
     * 刷新节点状态 
     */
    public refresh() {
        this.redpointStyle && this.redpointStyle.refresh(this.redpointNums);
    }

    /**
     * 注册驱动节点的事件
     */
    private registerEvent() {
        if (!this.cfg) return;
        RedpointSystem.getInstance().on(this.cfg.name, this.addOrDecRedpointNum, this);
    }

    /**
     * 取消注册事件
     */
    private unregisterEvent() {
        if (!this.cfg) return;
        RedpointSystem.getInstance().off(this.cfg.name, this.addOrDecRedpointNum, this);
    }

    /**
     * 增加\减少红点
     * @param num 
     */
    public addOrDecRedpointNum(num: number) {
        this.redpointNums = Math.max(this.redpointNums + num, 0);
        this.refresh();
        if (this.parent) {
            this.parent.addOrDecRedpointNum(num);
        }
    }

    /**
     * 设置红点数量
     * @param num 
     */
    public setRedpointNum(num: number) {
        num = Math.max(num, 0);
        this.addOrDecRedpointNum(num - this.redpointNums);
    }

    /**
     * 添加节点
     * @param redPointNode 
     */
    public add(redPointNode: RedpointNode) {
        this.children.push(redPointNode);
        redPointNode.parent = this;
    }

    /**
     * 移除节点 
     * @param name 
     */
    public remove(name: string) {
        for (let i = 0, len = this.children.length; i < len; i++) {
            if (this.children[i].name == name) {
                this.children[i].unregisterEvent();
                this.children.splice(i, 1);
                i--;
                break;
            }
        }
    }

    /**
     * 查找节点
     * @param name 
     */
    public find(name: string): RedpointNode {
        for (let v of this.children) {
            if (v.name === name) return v;
        }
        return null;
    }

    /**
     * 添加redPointItem
     * @param target 
     */
    public setTarget(target: cc.Node) {
        if (!this.cfg) return;
        if (this.target) return RedpointSystem.getInstance().debug && cc.log("重复注册红点", this.cfg.name);
        this.target = target;
        if (this.cfg.style !== "") {
            cc.loader.loadRes(this.cfg.style, cc.Prefab, (err, res) => this.onRedpointStyleLoaded(err, res));
        }
    }

    /**
     * 移除redPointItem
     * @param target 
     */
    public removeTarget() {
        if (this.redpointStyle && this.redpointStyle.node && this.redpointStyle.node.isValid) {
            this.redpointStyle.node.destroy();
        }
        this.target = null;
        this.redpointStyle = null;
    }

    private onRedpointStyleLoaded(err, res) {
        if (err) {
            RedpointSystem.getInstance().debug && cc.log(err);
        } else if (this.target && this.target.isValid && !this.redpointStyle) {
            let node = cc.instantiate(res) as cc.Node;
            node.parent = this.target;
            let styleCom = node.getComponent(RedpointStyle);
            this.redpointStyle = styleCom;
            let index = this.cfg.x.indexOf("%");
            if (index !== -1) {
                this.cfg.x = (parseFloat(this.cfg.x.slice(0, index)) * 0.01 - this.target.anchorX) * this.target.width;
            } else {
                this.cfg.x = parseFloat(this.cfg.x);
            }
            index = this.cfg.y.indexOf("%");
            if (index !== -1) {
                this.cfg.y = (parseFloat(this.cfg.y.slice(0, index)) * 0.01 - this.target.anchorY) * this.target.height;
            } else {
                this.cfg.y = parseFloat(this.cfg.y);
            }

            this.refresh();
        }
    }
}
